<?php
$user = new User();
$userName = getInput("userName");
//$email = getInput("email");
$pass = getInput("password");
//keep they in the session
$_SESSION['username'] = getInput("userName");
$_SESSION['password'] = getInput("password");

$remember = getInput("rememberme", true);
if ($remember){
	$remember = true;
}

$redirect= $_SERVER["HTTP_REFERER"];
$errors = array();
if ($userName == ""){
	$errors[] = array("name" => "userName", "error" => langEcho("usernameIsEmpty"));
}
 if($pass == ""){
 	$errors[] = array("name" => "password", "error" => sprintf(langEcho("passwordLengthError"), MAXPASSLENGTH, MINPASSLENGTH));
}

if($errors){
	echoJsonResponse(false, "", "","",$errors);
}

else{
	$result = $user->login($userName, $pass,$remember, $lead = false);
	if($result === true){
		$user->incrementBuilderLogins();
		logSuccess(langEcho("user:login:success"));
		if($_REQUEST["redirect"] != ''){
			$redirect = $_REQUEST["redirect"];
		}else{
			$redirect = WWWROOT . "home";
		}
		echoJsonResponse(true, $redirect);
	} else {
		//if(authenticateUser($userName, $pass)){
		//	echoJsonResponse(false, "", langEcho("user:login:error:lead"));
		//}else{

			if(!$result){
				$errors[] = array("name" => "userName", "error" => "");
				$errors[] = array("name" => "password", "error" => langEcho("user:login:error"));
				echoJsonResponse(false, "", "","",$errors);
			}else{
				echoJsonResponse(false, "", $result);
			}
		//}
	}
}
?>
